package Utils.TreadLockSupport;

import java.util.concurrent.locks.LockSupport;

/**
 * @author: Laity
 * @Project: JavaLaity
 * @Package: laity.interview.ThreadExchange.TO2_00_LockSupport
 * @Date: 2022年09月19日 13:12
 * @Description: 线程交换输出问题
 */
public class TO2_00_LockSupport {

    int[] nums = new int[5];


    static Thread t1 = null, t2 = null;

    public static void main(String[] args) {
        char[] aI = "123456".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();

        t1 = new Thread(() -> {
            for (char a : aI) {
                System.out.print(a + " ");
                LockSupport.unpark(t2); // 唤醒t2
                LockSupport.park();     // t1进行睡眠 - 阻塞，等待被唤醒
            }
        });

        t2 = new Thread(() -> {
            for (char c : aC) {
                LockSupport.park();   // t2挂起
                System.out.print(c + " ");
                LockSupport.unpark(t1); // 唤醒t1
            }
        });

        t1.start();
        t2.start();
    }
}
